<?php

/**
 * ObjectModel for fiche database table. 
 * @author Nicolas Legrand <n.legrand28@gmail.com>
 */
class FicheAbo extends ObjectModel
{
	
	public $id_abo;
	public $id_customer;
	public $customer_firstname;
	public $customer_lastname;
	public $customer_email;
	public $date_start;
	public $date_end;
	public $storeSize;
	public $twoInOne;
	
	public static $definition = array(
	
  		'table' 	=> 'ficheAbo',
  		'primary' 	=> 'id_abo',
  		'multilang' => false,
 		'fields' => array(
   			'id_abo' => array(
   				'type' => ObjectModel :: TYPE_INT
			),
			'id_customer' => array(
   				'type' => ObjectModel :: TYPE_INT, 
   				'required' => true
			),
			'customer_firstname' => array(
   				'type' => ObjectModel :: TYPE_STRING, 
   				'required' => true
			),
			'customer_lastname' => array(
   				'type' => ObjectModel :: TYPE_STRING, 
   				'required' => true
			),
			'customer_email' => array(
   				'type' => ObjectModel :: TYPE_STRING, 
   				'required' => true
			),
			'storeSize' => array(
   				'type' => ObjectModel :: TYPE_INT, 
   				'required' => true
			),
			'twoInOne' => array(
   				'type' => ObjectModel :: TYPE_INT, 
   				'required' => true
			),
			'date_start' => array(
   				'type' => ObjectModel :: TYPE_DATE, 
   				'required' => false
			),
			'date_end' => array(
   				'type' => ObjectModel :: TYPE_DATE, 
   				'required' => false
			)
  		)
	);
	
	public static function getFicheAbo($customerId)
	{
		$abo = FicheAbo::findAboByCustomerId($customerId);
		if ( $abo == null )
		{
			$currentCustomer = new Customer($customerId);
			
			if ( $currentCustomer->id == null)
				return null;
			
			$abo = new FicheAbo();
			$abo->customer_firstname = $currentCustomer->firstname;
			$abo->customer_lastname = $currentCustomer->lastname;
			$abo->customer_email = $currentCustomer->email;
			$abo->id_customer = $customerId;
			$abo->date_start = date('Y-m-d', time());
			$abo->date_end = date('Y-m-d', time() + (3600 * 24));
			$abo->storeSize = 5 * 1024 * 1024;
			$abo->twoInOne = 0;
			$abo->add();
		}
		
		return $abo;
	}
	
	public static function setAboEndBy($customerId, $value)
	{
		$value = time() + $value;
		$abo = FicheAbo::getFicheAbo($customerId);
		
		if ( $abo != null && $value > 0)
		{
			$abo->date_end = date('Y-m-d', $value);
			$abo->update();
		}
	}
	
	public static function buildWhereClause($conditions)
	{
		$whereClause = '';
		$i = 0;
		
		foreach( $conditions as $key => $value )
		{
			$whereClause .= ($i == 0) ? ' WHERE ' : ' AND ';
			
			if ( is_array($value) )
			{
				if ( $value['operator'] == 'LIKE' )
				{
					$whereClause .= $value['column']." LIKE '%".$value['value']."%'";
				}
				else
				{
					$whereClause .= $value['column']." ".$value['operator']." '".$value['value']."'";
				}
			}
			else
			{
				$whereClause .= $key." = '".$value."'";
			}
			$i++;
		}
		
		return $whereClause;
	}
	
	public static function findAboByCustomerId($id)
	{
		$abos = FicheAbo::findAboBy(array('id_customer' => (int)($id)));
		if ( count($abos) > 0)
		{
			return $abos[0];
		}
		
		return null;
	}
	
	public static function getTotalAboWhere($conditions)
	{
		$sql = 'SELECT COUNT(id_abo) as total FROM ' . _DB_PREFIX_ . 'ficheAbo '.Fiche::buildWhereClause($conditions);
		$total = 0;
		if ($rows = Db :: getInstance()->ExecuteS($sql)) {
			$total = (int)($rows[0]['total']);
		}
			
		return $total;
	}
	
	public static function findAboBy( $conditions, $start = -1, $end = -1, $orderBy = null, $asc = true)
	{
		$sql = 'SELECT * FROM ' . _DB_PREFIX_ . 'ficheAbo '.Fiche::buildWhereClause($conditions);
		
		if ( $start != -1 || $end != -1)
		{
			$sql .= ' LIMIT '.$start.', '.$end;
		}
		
		if ( $orderBy != null )
		{
			$sens = ( $asc == true ) ? 'ASC':'DESC';
			$sql .= ' ORDER BY '.$orderBy.' '.$sens;
		}
		
		if ($rows = Db :: getInstance()->ExecuteS($sql)) {
			return ObjectModel :: hydrateCollection(__CLASS__, $rows);
		}
		return array();
	}
	
}